package com.ruoyi.framework.aspectj;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.eamp.domain.EampSrLog;
import com.ruoyi.eamp.mapper.EampSrLogMapper;
import com.ruoyi.eamp.service.IEampSrLogService;
import com.ruoyi.eamp.service.impl.EampSrLogServiceImpl;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Aspect
@Component
public class EampSrRemoveAspect {

    @Autowired
    private IEampSrLogService eampSrLogService;

    @Pointcut("@annotation(com.ruoyi.common.annotation.IEampSrRemoveLog)")
    public void removeLogPointcut() {
    }

    @Around("removeLogPointcut()")
    public Object logSrRemoval(ProceedingJoinPoint joinPoint) throws Throwable {
        Long userId = SecurityUtils.getUserId();
        LocalDateTime localDate = LocalDateTime.now();
        String methodName = ((MethodSignature) joinPoint.getSignature()).getMethod().getName();

        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            if (arg instanceof Long[]) { // 检查是否为 Long 数组  
                Long[] srIds = (Long[]) arg;
                for (Long srId : srIds) {
                    EampSrLog eampSrLog = new EampSrLog(null, userId, localDate, methodName, srId,null, 1L);
                    eampSrLogService.insertEampSrLog(eampSrLog);
                }
            }
        }

        return joinPoint.proceed();
    }
}  